package com.situ.company.department.Servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.situ.company.department.Servlet.DepartmentServlet;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.DepartmentService;
import com.situ.company.util.FmtRequest;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet{
	
	private static final long serialVersionUID = 1L;
	private DepartmentService service = new DepartmentService();
	
//	@Override
//	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		upd2(req);
//		req.getRequestDispatcher("/web/page/department/upd2.jsp").forward(req, resp);
//	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub	
		String action = req.getParameter("action");
		String result="";
		switch(action) {
			case "add":
				result = add(req);
				break;
			case "sel":
				result = sel(req);
				break;
			case "del":
				result = del(req);
				break;
			case "get":
				result = get(req);
				break;
			case "upd":
				result = upd(req);
				break;
			case "upd2":	// 请求转发方式回显
				upd2(req);
				req.getRequestDispatcher("/web/page/department/upd2.jsp").forward(req, resp);
				break;
			case "list":
				result = list(req);
				break;
			default:
				break;	
		}
	
		FmtRequest.out(resp,result);
		
	}

	private String list(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		String pageIndex =req.getParameter("pageIndex");
		String pageLimit =req.getParameter("pageLimit");
		DepartmentModel model = new DepartmentModel(code, name);	
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		
		
//		List<DepartmentModel> list = service.selectList(model);
//		int count = service.selectCount(model);
//		分页控件渲染时的count使用的是分页前的记录条数
//		分页后的记录条数=带有查询条件的并带有limit的查询结果的记录条数(<=10)
//		分页前的记录条数=带有查询条件的不带有limit的查询结果的记录条数(>=10)
		Map<String,Object> map = new HashMap<>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return new JSONObject(map).toString();
	}

	private void upd2(HttpServletRequest req) {
		String code = req.getParameter("code");
		DepartmentModel model = new DepartmentModel(code);
		DepartmentModel mod = service.selectModel(model);
		req.setAttribute("mdb", mod);
	}

	private String upd(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		String tel =req.getParameter("tel");
		DepartmentModel model = new DepartmentModel(code, name, tel);
		
//		Map<String,String> fields = new HashMap<>();
//		fields.put("code", "code");
//		fields.put("name", "name");
//		fields.put("tel", "tel");
//		DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
		
//		DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class);
		return service.update(model);
	}

	private String get(HttpServletRequest req) {
		String code = req.getParameter("code");
		DepartmentModel model = new DepartmentModel(code);
		DepartmentModel mod = service.selectModel(model);
		return new JSONObject(mod).toString();
	}

	private String del(HttpServletRequest req) {
		String code = req.getParameter("code");
		DepartmentModel model = new DepartmentModel(code);		
		return service.delete(model);
	}

	private String sel(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		DepartmentModel model = new DepartmentModel(code, name);	
		List<DepartmentModel> list =  service.selectList(model);	
		return new JSONArray(list).toString();
	}

	

	private String add(HttpServletRequest req) {
		String code =req.getParameter("code");
		String name =req.getParameter("name");
		String tel =req.getParameter("tel");
		DepartmentModel model = new DepartmentModel(code, name, tel);
		return service.insert(model);

	}
	
}
